/**
 * com.monte.service.TreeEntityService.java
 * create by FengMy from 2013
 */
package com.monte.service;

import java.util.List;

import javax.persistence.TypedQuery;

import com.monte.entity.TreeEntity;
import com.monte.util.StringUtils;

/**
 * @描述:
 * @author FengMy
 * @since 2013-3-25
 */
@SuppressWarnings("rawtypes")
public class TreeEntityService<T extends TreeEntity> extends DataService<T> {

	@Override
	public T addEntity(T entity) {
		TreeEntity parent = entity.getParent();
		entity.setLongNumber(entity.getNumber());
		if(parent != null){
			parent = getEntityById(parent.getId());
			if(parent != null){
				entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
			}
		}
		return super.addEntity(entity);
	}

	@Override
	public T updateEntity(T entity) {
		TreeEntity oldData = getEntityById(entity.getId());
		if(oldData != null){
			if(!StringUtils.isEmpty(oldData.getNumber()) && oldData.getNumber().equals(entity.getNumber())){
				//修改了编码,需要更新长编码
				TreeEntity parent = entity.getParent();
				if(parent != null){
					parent = getEntityById(parent.getId());
					if(parent != null){
						entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
					}
				}
				//查询下级节点的JPQL
				String getChildSql = "SELECT e FROM " 
						+ entity.getClass().getName() + " e " 
						+ "WHERE e.longNumber LIKE '"+oldData.getLongNumber()+"!%'";
				@SuppressWarnings("unchecked")
				List<T> children = (List<T>) persistenceManager.getEntityManager().createQuery(getChildSql, entity.getClass()).getResultList();
				if(children != null && children.size() > 0){
					for(TreeEntity t : children){
						if(!StringUtils.isEmpty(t.getLongNumber())){
							t.setLongNumber(t.getLongNumber().replaceFirst(oldData.getLongNumber(), entity.getLongNumber()));
						}
					}
					updateBatch(children);
				}
			}
		}
		return super.updateEntity(entity);
	}

	@Override
	public int deleteEntity(T entity) {
		entity = getEntityById(entity.getId());
		if(!entity.allowDeleteHasChild()){//如果不允许在存在子节点的情况下删除,则检查是否存在子节点
			if(entity != null && hasDirectChildren(entity)){
				return 0;
			}
		}
		int result = super.deleteEntity(entity);
		if(entity.getParent() != null && !hasDirectChildren(entity.getParent())){//更新上级节点是否叶子节点
			T parent = getEntityById(entity.getParent().getId());
			if(parent != null){
				parent.setLeaf(true);
				super.updateEntity(parent);
			}
		}
		return result;
	}
	
	/**
	 * 是否拥有直接下级节点
	 * @param entity
	 * @return
	 */
	private boolean hasDirectChildren(TreeEntity entity){
		//查询所有下级节点数的JPQL
		String getChildSql = "SELECT COUNT(1) FROM " 
				+ entity.getClass().getName() + " e " 
				+ "WHERE e.parent = :parent";
		TypedQuery<Long> query = persistenceManager.getEntityManager().createQuery(getChildSql, Long.class);
		query.setParameter("parent", entity);
		Long count = query.getSingleResult();
		return count > 0;
	}
	
}
